<?php
class ScheduleRegistriesController extends AppController {

	var $name = 'ScheduleRegistries';
	var $paginate = array(
				'limit' => 10,
				//'group' => array('ScheduleRegistry.schedule_id'), 
				'order' => array(
				//'ScheduleRegistry.id' => 'desc',
				'ScheduleRegistry.schedule_id' => 'asc',
				'ScheduleRegistry.schedule_type_id' => 'asc',
				'ScheduleRegistry.created' => 'asc'
				
				)
				); 

	function index($schedule_id = null) {
		$this->ScheduleRegistry->recursive = 0;
		if($schedule_id != null){
			$this->paginate['conditions'] = array('schedule_id' => $schedule_id);
			$this->set('select_schedule_id',$schedule_id);
			$this->set('schedule_view',$this->ScheduleRegistry->Schedule->find('all',array('conditions'=>array('Schedule.id'=>$schedule_id))));
			
		}
		else {
			$get_Schedule = $this->ScheduleRegistry->Schedule->find('all',array('conditions'=>array('Schedule.active'=>1)));
			$this->paginate['conditions'] = array('schedule_id' => $get_Schedule[0]['Schedule']['id']);
			$this->set('select_schedule_id',$get_Schedule[0]['Schedule']['id']);
			$this->set('schedule_view',$this->ScheduleRegistry->Schedule->find('all',array('conditions'=>array('Schedule.id'=>$get_Schedule[0]['Schedule']['id']))));
		}
		$this->set('get_schedules',$this->ScheduleRegistry->Schedule->find('all'));
		$this->set('scheduleRegistries', $this->paginate());
		$this->set('schedule',$this->ScheduleRegistry->Schedule->find('list'));
	}
	
	
	
/*
		function index($schedule_id = null) {
		$this->ScheduleRegistry->recursive = 0;
		if($schedule_id != null){
			$this->paginate['conditions'] = array('schedule_id' => $schedule_id);
			$this->set('select_schedule_id',$schedule_id);
		}
		else
			$this->paginate();
		$this->set('get_schedules',$this->ScheduleRegistry->Schedule->find('all'));
		$this->set('scheduleRegistries', $this->paginate());
		$this->set('schedule',$this->ScheduleRegistry->Schedule->find('list'));
	}
	*/
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid schedule registry', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('scheduleRegistry', $this->ScheduleRegistry->read(null, $id));
	}

	function add($schedule_id = null) {
		if (!empty($this->data)) {
			$this->ScheduleRegistry->create();
			if ($this->ScheduleRegistry->save($this->data)) {
				$this->Session->setFlash(__('The schedule registry has been saved', true));
				$this->redirect(array('action' => 'index/'.$this->data['ScheduleRegistry']['schedule_id']));
			}else {
				$this->Session->setFlash(__('The schedule registry could not be saved. Please, try again.', true));
			}
		}
		if($schedule_id != null)
			$schedules = $this->ScheduleRegistry->Schedule->find('list',array('conditions'=>array('Schedule.id'=>$schedule_id)));
		$scheduleTypes = $this->ScheduleRegistry->ScheduleType->find('list');
		$this->set(compact('schedules', 'scheduleTypes'));
	}
	function set_add($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Dữ liệu không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->ScheduleRegistry->create();
			if ($this->ScheduleRegistry->save($this->data)) {
				$this->Session->setFlash(__('The schedule registry has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The schedule registry could not be saved. Please, try again.', true));
			}
		}
		$schedules = $this->ScheduleRegistry->Schedule->find('list');
		$scheduleTypes = $this->ScheduleRegistry->ScheduleType->find('list');
		$this->set(compact('schedules', 'scheduleTypes'));
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Dữ liệu không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if($this->data['ScheduleRegistry']['start_time'] > $this->data['ScheduleRegistry']['start_time']){
					$this->Session->setFlash(__('Thời gian bắt đầu > thời gian kết thúc: ('.$this->data['ScheduleRegistry']['start_time'] .' > '. $this->data['ScheduleRegistry']['start_time'].')', true));
					$this->redirect(array('action'=>'edit/'.$this->data['ScheduleRegistry']['id']));
				}
			if ($this->ScheduleRegistry->save($this->data)) {
				$this->Session->setFlash(__('Lịch đăng ký đã được lưu', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Lịch đăng ký chưa được lưu, hãy kiểm tra lại', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ScheduleRegistry->read(null, $id);
		}
		$schedules = $this->ScheduleRegistry->Schedule->find('list');
		$scheduleTypes = $this->ScheduleRegistry->ScheduleType->find('list');
		$this->set(compact('schedules', 'scheduleTypes'));
	}
	
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for schedule registry', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ScheduleRegistry->delete($id)) {
			$this->Session->setFlash(__('Schedule registry deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Schedule registry was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	/**Set active cho một lịch
	* Khi chọn 1 năm active đăng ký lịch cho một kiểu
	* lịch nào đó thì trường active được thực thị
	* Các dữ liệu khác với điều kiện đó sẽ ko thực thi
	*/
	 function set_active($id = null){
		if(!$id){
			$this->Session->setFlash(__('Lịch đăng ký không tồn tại',true));
			$this->redirect(array('action'=>'index'));
		}
		$get_link ='';
		if(isset($this->passedArgs['get_link'])){
			$get_link = $this->passedArgs['get_link'];
		}
		$get_ScheduleRegistry= $this->ScheduleRegistry->find('all');
		if(isset($this->passedArgs['status_schelude_type'])){
			$get_schedule_type = $this->passedArgs['status_schelude_type'];
			foreach($get_ScheduleRegistry as $select_schedule){
				$this->data['ScheduleRegistry']['id'] = $select_schedule['ScheduleRegistry']['id'];
				if($select_schedule['ScheduleRegistry']['id']== $id && $get_schedule_type == $select_schedule['ScheduleRegistry']['schedule_type_id']){
					$this->data['ScheduleRegistry']['status']= '1';
					$this->ScheduleRegistry->save($this->data);
				}
				else if($get_schedule_type == $select_schedule['ScheduleRegistry']['schedule_type_id']){
					$this->data['ScheduleRegistry']['status']= '0';
					$this->ScheduleRegistry->save($this->data);
				}
				$this->data=null;//Set data to null
				
			}
		}
		$this->Session->setFlash(__('Trạng thái đã được kích hoạt',true));
		$this->redirect(array('action'=>'index/'.$get_link));
		
    }
	
	function active_status_off($id = null) {
		if(!$id){
			$this->Session->setFlash(__('Lịch đăng ký không tồn tại',true));
			$this->redirect(array('action'=>'index'));
		}
		$get_link ='';
		if(isset($this->passedArgs['get_link'])){
			$get_link = $this->passedArgs['get_link'];
		}
		if ($this->ScheduleRegistry->saveField('status','0')) {
			$this->Session->setFlash(__('Trạng thái được tắt', true));
			$this->redirect(array('action' => 'index'));
		}
		else{
			$this->Session->setFlash(__('Không thể tắt trạng thái này. Hãy kiểm tra lại', true));
			$this->redirect(array('action' => 'index/'.$get_link));
		}
	}
}
?>